<?php

namespace Admin\Controller;
use Admin\Model\UserLogModel;

class ShareController extends MainController{
    /**
     * 检查商品获奖用户
     * @queyuan
     * 2016/07/15
     * @param $sale_id,$user_id
     */
    public function checkUser(){
        $sale_id = I("param.sale_id");
        $user_id = I("param.user_id");
        $result = array();
        if(!$sale_id || !$user_id){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $sale = M("sales s")->join("left join one_order_code c on (s.code_id = c.id) left join one_order o on (c.order_id = o.id) left join one_goods g on (s.good_id = g.id)")->where("s.id = ".$sale_id)->field("o.user_id,g.name as good_name,g.index_img,s.win_time,c.code,s.id as sale_id,o.id as order_id,c.id as code_id")->find();
            if(!$sale){
                $result['status'] = 1;
                $result['msg'] = "无此已开奖商品";
            }else{
                if($sale['user_id'] != $user_id){
                    $result['status'] = 1;
                    $result['msg'] = "用户错误";
                }else{
                    $result['status'] = 0;
                    $result['msg'] = "请求成功";
                    $result['sale'] = $sale;
                }
            }
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 晒单
     * @queyuan
     * 2016/07/15
     * @param $content,$img,$code_id,$user_id,$order_id,$sale_id,$ip
     */
    public function addShare(){
        $content = I("param.content");
        $img = I("param.img");
        $code_id = I("param.code_id");
        $user_id = I("param.user_id");
        $order_id = I("param.order_id");
        $sale_id = I("param.sale_id");
        $ip = I("param.ip");
        $result = array();
        if(!$content ||  !$code_id || !$user_id || !$order_id || !$sale_id){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $sale = M("sales s")->join("left join one_order_code c on (s.code_id = c.id) left join one_order o on (c.order_id = o.id) left join one_goods g on (s.good_id = g.id)")->where("s.id = ".$sale_id)->field("o.user_id,g.name as good_name,g.index_img,s.win_time,c.code,s.id as sale_id,o.id as order_id,c.id as code_id")->find();
            if(!$sale){
                $result['status'] = 1;
                $result['msg'] = "无此已开奖商品";
            }else{
                if(($sale['order_id'] != $order_id) || ($sale['user_id'] != $user_id) || ($sale['code_id'] != $code_id)){
                    $result['status']  = 1;
                    $result['msg'] = "非法操作";
                }else{
                    $data = array(
                        'user_id'=>$user_id,
                        'order_id'=>$order_id,
                        'sale_id'=>$sale_id,
                        'content'=>$content,
                        'img'=>$img,
                        'status'=>1,
                        'code_id'=>$code_id,
                        'share_time'=>time()
                    );
                    $res = M("share")->add($data);
                    if(!$res){
                        $result['status'] = 1;
                        $result['msg'] = "晒单失败";
                    }else{
                        $result['status'] = 0;
                        $result['msg'] = "晒单成功";
                        $result['id'] = $res;
                    }
                }
            }
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 晒单详情
     * @queyuan
     * 2016/07/15
     * @param $id
     */
    public function shareDetail(){
        $id = I("param.id");
        $result = array();
        if(!$id){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $detail = M("share a")->join("left join one_sales s on (a.sale_id = s.id) left join one_user u on (a.user_id = u.id) left join one_order_code c on (a.code_id = c.id) left join one_goods g on (s.good_id = g.id)")->where("a.id = ".$id)->field("a.share_time,a.code_id,a.user_id,a.sale_id,a.order_id,c.code,s.win_time,g.name as good_name,u.name as username,a.img,a.content,u.avatar,u.avatar_type,s.good_id")->find();
            if(!$detail){
                $result['status'] = 1;
                $result['msg'] = "无此晒单";
            }else{
                $detail['img'] = explode(",",$detail['img']);
                $result['status'] = 0;
                $result['msg'] = "请求成功";
                $result['detail'] = $detail;
            }
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 晒单列表
     * @queyuan
     * 2016/07/16
     * @param $id
     */
    public function shareList(){
        $id = I("param.id");
        $result = array();
        if(!$id){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $good_id = M("sales")->where("id = ".$id)->getField("good_id");
            $list = M("share s")->join("left join one_sales os on (s.sale_id = os.id) left join one_user u on (s.user_id = u.id) left join one_goods g on (os.good_id = g.id)")->where("os.good_id = ".$good_id)->field("s.content,u.name as user_name,s.img,s.share_time,g.name as good_name")->order("s.share_time desc")->select();
            foreach ($list as &$item) {
                $item['img'] = explode(",",$item['img']);
            }
            $result['status'] = 0;
            $result['msg'] = "请求成功";
            $result['list'] = $list;
        }
        echo json_encode($result);
        exit;
    }
}
?>